﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace KinematicCharacterController.Examples
{
    public class ExampleAIController : MonoBehaviour
    {
        public float MovementPeriod = 1f;
        public List<ExampleCharacterController> Characters = new List<ExampleCharacterController>();

        private bool _stepHandling;
        private bool _ledgeHandling;
        private bool _intHandling;
        private bool _safeMove;

        private bool _isUsedRigidBody;

        public void SetMotorRigidBodyEnable(bool value)
        {
            _isUsedRigidBody = value;
        }

        private void Update()
        {
            AICharacterInputs inputs = new AICharacterInputs();

            // Simulate an input on all controlled characters
            //inputs.MoveVector = Mathf.Sin(Time.time * MovementPeriod) * Vector3.forward;
            inputs.LookVector = Vector3.Slerp(-Vector3.forward, Vector3.forward, inputs.MoveVector.z).normalized;
            for (int i = 0; i < Characters.Count; i++)
            {
                int flag = Random.Range(1, 4);

                switch (flag)
                {
                    case 1:
                        inputs.MoveVector = Mathf.Sin(Time.time * MovementPeriod) * Vector3.forward;
                        break;
                    case 2:
                        inputs.MoveVector = Mathf.Sin(Time.time * MovementPeriod) * Vector3.back;
                        break;
                    case 3:
                        inputs.MoveVector = Mathf.Sin(Time.time * MovementPeriod) * Vector3.left;
                        break;
                    case 4:
                        inputs.MoveVector = Mathf.Sin(Time.time * MovementPeriod) * Vector3.right;
                        break;
                }

                if (_isUsedRigidBody)
                {
                    var rigidBody = Characters[i].GetComponent<Rigidbody>();
                    if (rigidBody != null)
                    {
                        rigidBody.AddForce(inputs.MoveVector * 5.0f);

                        /*float speed = 2.0f; 
                        switch (flag)
                        {
                            case 1:
                                rigidBody.velocity = Vector3.forward * speed; 
                                break;
                            case 2:
                                rigidBody.velocity = Vector3.back * speed;
                                break;
                            case 3:
                                rigidBody.velocity = Vector3.left * speed;
                                break;
                            case 4:
                                rigidBody.velocity = Vector3.right * speed;
                                break;
                        }*/
                    }
                }

                Characters[i].SetInputs(ref inputs);
            }
        }
    }
}